package com.yunheone.view.Student;

import com.yunheone.control.Studentcontrol;
import com.yunheone.control.Teachcon;
import com.yunheone.mode.StudentUse;
import com.yunheone.mode.TeacherUser;

import java.util.Scanner;

/**
 * Created by Administrator on 2017/8/9 0009.
 */
public class StudentAdd {

    public static StudentUse studentUse(){
        System.out.println ("**************学生管理系统—添加学生**************");
        System.out.println ("请输入要添加的学生信息：");
        Scanner input=new Scanner ( System.in );
        System.out.print ("请输入学生班级");
        String grade=input.next ();
        System.out.print ("请输入学生学号");
        String id=input.next ();
        System.out.print ("请输入学生姓名");
        String name=input.next ();
        System.out.print ("请输入学生年龄");
        String age=input.next ();
        System.out.print ("请输入学生性别(男/女):");
        String sex=input.next ();
        try{
            if (Studentcontrol.getStudentmap ().get ( id )!=null&&Studentcontrol.getStudentmap ().get ( id ).getGrade ().equals ( grade )){
                //首先判断原集合id是否为空，为空会爆出空指针异常，不为空再判断班级是否相同
                if (Studentcontrol.getStudentmap ().containsKey ( id )){   //班级相同，编号也相同，抛出异常
                    throw new Exception ("编号和班级已存在，请重新输入：");
                }
            }else if (!sex.equals ( "男" )&&!sex.equals ( "女" )){
                throw new Exception ( "性别输入有误，请重新输入" );
            }
            return new StudentUse ( grade,id,name,age,sex );
        }catch (Exception e){
            String message = e.getMessage ();
            System.out.println (message!=null?message:"输入不合法");
            return studentUse ();
        }
    }
}
